﻿using System.Windows.Forms;
using MobileCommissionManagement.Common;

namespace MobileCommissionManagement.VodafoneControls
{
    public partial class VodafoneDealerSetup : Form
    {
        private ConfigurarDealer _dealerSetup;
        private VodafoneController _controller;
        private int _newId = -1;

        #region Properties

        public VodafoneController VodafoneController
        {
            set
            {
                _controller = value;
                businessTypeLookUpEdit.Properties.DataSource = value.BusinessTypes;                
            }
        }

        public ConfigurarDealer Document
        {
            set
            {
                _dealerSetup = value;
                configurarDealerBindingSource.DataSource = _dealerSetup;
                configurarDealerColunasBindingSource.DataSource = _dealerSetup.ConfigurarDealerColunas;
                businessLineLookUpEdit.Properties.DataSource = _controller.GetBusinessLines(_dealerSetup.Tipo);
                columnsRepositoryItemComboBox.Items.AddRange(_controller.GetDealerColumns(_dealerSetup.Tipo));
            }
            get { return _dealerSetup; }
        }

        #endregion

        public VodafoneDealerSetup()
        {
            InitializeComponent();
            gridView.InitNewRow += GridViewInitNewRow;
            businessTypeLookUpEdit.EditValueChanging += BusinessTypeLookUpEditEditValueChanging;
        }

        void BusinessTypeLookUpEditEditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            if(e.OldValue == null) return;
            _dealerSetup.LinhaNegocio = string.Empty;
            _dealerSetup.ConfigurarDealerColunas.Clear();
            businessLineLookUpEdit.Properties.DataSource = _controller.GetBusinessLines(e.NewValue.ToString());
            columnsRepositoryItemComboBox.Items.Clear();
            columnsRepositoryItemComboBox.Items.AddRange(_controller.GetDealerColumns(e.NewValue.ToString()));                            
        }

        void GridViewInitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            var dealerSetupColumns = (ConfigurarDealerColunas)gridView.GetRow(e.RowHandle);
            dealerSetupColumns.IdConfiguracaoColuna = _newId--;            
        }
        
    }
}
